﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.KyLuatSinhVienService;
namespace eTMS.Controllers{
public class KyLuatSinhVienControllerBase : CommonController{
	public KyLuatSinhVienControllerBase()
		: base()
	{	
		this.dao = new KyLuatSinhVienServiceClient();
	}
	~KyLuatSinhVienControllerBase(){
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	private DataTable _Table;
	private KyLuatSinhVienEntity oKyLuatSinhVien;	
	public KyLuatSinhVienEntity KyLuatSinhVienObject{
		get{
			return oKyLuatSinhVien;
		}
		set{
			oKyLuatSinhVien=value;
		}
	}
	protected KyLuatSinhVienServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSKyLuatSinhVien(false);			
			return _Table;
		}
	}
	public void LoadAllDSKyLuatSinhVien(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oKyLuatSinhVien==null)return false;
		bool result=true;
		try{
			oKyLuatSinhVien = dao.SaveRet(oKyLuatSinhVien);
		}
		catch{
			result=false;
		}
		if(result){
			_ListKyLuatSinhVien.Add(oKyLuatSinhVien);
		}
		return result;
	}
	public bool Update(){
		if(oKyLuatSinhVien==null)return false;
		return dao.Update(oKyLuatSinhVien);
	}
	public bool Delete(){
		if(oKyLuatSinhVien==null)return false;
		bool retvalue = dao.Delete(oKyLuatSinhVien);
		if(retvalue){			
			_ListKyLuatSinhVien.Remove(oKyLuatSinhVien);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 KyLuatSinhVien_ID){
		bool retvalue = dao.DeleteByID(KyLuatSinhVien_ID);		
		if(retvalue){
			KyLuatSinhVienEntity oDeleted=FindKyLuatSinhVien(KyLuatSinhVien_ID);
			if(oDeleted!=null){
				_ListKyLuatSinhVien.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oKyLuatSinhVien = new KyLuatSinhVienEntity();
	}
    //Copy data from data row in grid to object
    public KyLuatSinhVienEntity MapDataRowToObject(DataRow row){
		KyLuatSinhVienEntity o = new KyLuatSinhVienEntity();
		 if(!row.IsNull("NgayKyLuat"))
			o.NgayKyLuat=ConvertDateTime(row["NgayKyLuat"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("HinhThucKyLuat_ID"))
			o.HinhThucKyLuat_ID=(Int32)(row["HinhThucKyLuat_ID"]);
		 if(!row.IsNull("HocKy_ID"))
			o.HocKy_ID=(Int32)(row["HocKy_ID"]);
		 if(!row.IsNull("NamHoc_ID"))
			o.NamHoc_ID=(Int32)(row["NamHoc_ID"]);
		 if(!row.IsNull("SinhVien_ID"))
			o.SinhVien_ID=(Int32)(row["SinhVien_ID"]);
		 if(!row.IsNull("KyLuatSinhVien_ID"))
		    o.KyLuatSinhVien_ID=(Int32)(row["KyLuatSinhVien_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(KyLuatSinhVienEntity o,DataRow row){
		row["NgayKyLuat"]=o.NgayKyLuat;
		row["GhiChu"]=o.GhiChu;
		row["HinhThucKyLuat_ID"]=o.HinhThucKyLuat_ID;
		row["HocKy_ID"]=o.HocKy_ID;
		row["NamHoc_ID"]=o.NamHoc_ID;
		row["SinhVien_ID"]=o.SinhVien_ID;
		row["KyLuatSinhVien_ID"]=o.KyLuatSinhVien_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.KyLuatSinhVienObject,row);
    }
	public void LoadObject(Int32 KyLuatSinhVien_ID){
		KyLuatSinhVienEntity oLoad=FindKyLuatSinhVien(KyLuatSinhVien_ID);
		if(oLoad==null){
			oKyLuatSinhVien=this.LoadKyLuatSinhVien(KyLuatSinhVien_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oKyLuatSinhVien.IsNullKyLuatSinhVien_ID;
			if(!bNotIsNull){
				this.ListKyLuatSinhVien.Add(oKyLuatSinhVien);
			}
		}
		else{
			oKyLuatSinhVien=oLoad;
		}
	}
	#region CachedList KyLuatSinhVien
	private static List<KyLuatSinhVienEntity> _ListKyLuatSinhVien=new List<KyLuatSinhVienEntity>();
    public List<KyLuatSinhVienEntity>ListKyLuatSinhVien{
		get{
			return _ListKyLuatSinhVien;
		}
	}
	public static List<KyLuatSinhVienEntity>ListKyLuatSinhVienStatic{
		get{
			return _ListKyLuatSinhVien;
		}
	}
	public KyLuatSinhVienEntity FindKyLuatSinhVien(Int32 KyLuatSinhVien_ID){
		KyLuatSinhVienEntity oResult=this.ListKyLuatSinhVien.Find(delegate(KyLuatSinhVienEntity oFind){
			bool result=true; 
			result&=oFind.KyLuatSinhVien_ID.Equals(KyLuatSinhVien_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllKyLuatSinhVien(bool clearCache){
		if(_ListKyLuatSinhVien.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		KyLuatSinhVienServiceClient dao = new KyLuatSinhVienServiceClient();
		_ListKyLuatSinhVien=new List<KyLuatSinhVienEntity>(dao.SelectAll());
		if(dao.State==System.ServiceModel.CommunicationState.Opened)try{dao.Close();}catch{}
	}
	public KyLuatSinhVienEntity LoadKyLuatSinhVien(Int32 KyLuatSinhVien_ID){			
		KyLuatSinhVienEntity o=dao.SelectOne(KyLuatSinhVien_ID);		
		return o;
	}
	#endregion
}
}
